package com.th.supcom.pds.impl.service.internal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.cxf.common.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.domain.vo.Result;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.common.enums.IsNeedCheck;
import com.th.supcom.pds.impl.common.utils.ServerUitls;
import com.th.supcom.pds.internal.api.AuditPrescService;
import com.th.supcom.pds.medicare.api.AdapterEmrPrescriptionService;
import com.th.supcom.pds.medicare.api.AdapterPtsExtOutPatientVisitService;

/**
 * @author yzhang
 */
public class AuditPrescServiceImpl implements AuditPrescService
{
    private TranslatorManager m_translatorManager;
    private AdapterEmrPrescriptionService m_adapterEmrPrescriptionService;
    private AdapterPtsExtOutPatientVisitService m_adapterPtsExtOutPatientVisitService;

    /**
     * 根据处方相关信息查询处方信息和患者信息
     */

    @Override
    public Result findPrescDetailByPrescNo (PdsSupplyVO pdsSupplyVO)
    {
        s_logger.info ("调用查询处方信息方法  findPrescDetailByPrescNo(PdsSupplyVO pdsSupplyVO  = 【{}】", new Object[]
        { pdsSupplyVO });
        // 存放处方列表和患者信息
        Result result = new Result ();
        List <PdsSupplyDetailVO> pdsSupplyDetailVOs = null;
        List <PdsSupplyVO> pdsSupplyVOs = null;
        pdsSupplyVOs = getPrescByPrescNo (pdsSupplyVO);
        if (CollectionUtils.isEmpty (pdsSupplyVOs))
        {
            throw new BusinessException ("此处方不存在，请重新查询！");
        }
        if (pdsSupplyVOs.get (0).getQualifiedType () != null)
        {
            throw new BusinessException ("此处方已审核！");
        }
        pdsSupplyDetailVOs = getPrescDetailByPrescNO (pdsSupplyVOs.get (0));
        // 查询药品是否需要审核
        String flag = ServerUitls.checkPhamsToxicology (pdsSupplyDetailVOs);
        if (IsNeedCheck.UnNeed.getEnumCode ().equals (ServerUitls.checkPhamsToxicology (pdsSupplyDetailVOs)))
        {
            s_logger.debug ("该处方没有毒麻精一类药品,不需要审核！");
            throw new BusinessException ("该处方不需要审核！");
        }
        pdsSupplyVOs.get (0).setCheckFlag (flag);

        String patientId = pdsSupplyVOs.get (0).getPatientId ();
        Integer prescSource = pdsSupplyVOs.get (0).getPrescSource ();

        PatientInfo patientInfo = m_adapterPtsExtOutPatientVisitService.getPatientInfo (null, prescSource, patientId);
        result.setPatientInfo (patientInfo);
        result.setPdsSupplyDetailVos (pdsSupplyDetailVOs);
        result.setPdsSupplyVOs (pdsSupplyVOs);
        return result;
    }

    /**
     * 根据处方号获取处方信息
     * 
     * @param pdsSupplyVO
     * @return
     */
    private List <PdsSupplyVO> getPrescByPrescNo (PdsSupplyVO pdsSupplyVO)
    {
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("prescNo", pdsSupplyVO.getPrescNo ());
        map.put ("type", "1");
        return m_adapterEmrPrescriptionService.findApplyAndPresc (map);
    }

    private List <PdsSupplyDetailVO> getPrescDetailByPrescNO (PdsSupplyVO pdsSupplyVO)
    {
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("id", pdsSupplyVO.getPrescId ());
        map.put ("type", "1");
        map.put ("prescNo", pdsSupplyVO.getPrescNo ());
        List <PdsSupplyDetailVO> pdsSupplyDetailVOs = m_adapterEmrPrescriptionService.findApplyAndDetailed (map);
        pdsSupplyDetailVOs = m_translatorManager.translate (pdsSupplyDetailVOs);
        ServerUitls.findPhamsCosts (pdsSupplyDetailVOs);
        return pdsSupplyDetailVOs;
    }

    @Override
    public String checkPresc (PdsSupplyVO pdsSupplyVO)
    {
        s_logger.info ("调用 处方审核方法  checkPresc(PdsSupplyVO pdsSupplyVO  = 【{}】", new Object[]
        { pdsSupplyVO });
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("prescNo", pdsSupplyVO.getPrescNo ());
        map.put ("empIdExec", pdsSupplyVO.getUserIdCheck ());
        map.put ("empNameExec", pdsSupplyVO.getUserNameCheck ());
        map.put ("orgIdExec", pdsSupplyVO.getOrgIdExec ());
        map.put ("orgNameExec", pdsSupplyVO.getOrgIdExecName ());
        map.put ("execAction", "1");
        map.put ("execResult", "1");
        map.put ("execNotes", pdsSupplyVO.getCheckMemo ());
        return m_adapterEmrPrescriptionService.checkPresc (map);
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    public void setAdapterEmrPrescriptionService (AdapterEmrPrescriptionService adapterEmrPrescriptionService)
    {
        m_adapterEmrPrescriptionService = adapterEmrPrescriptionService;
    }

    public void setAdapterPtsExtOutPatientVisitService (AdapterPtsExtOutPatientVisitService adapterPtsExtOutPatientVisitService)
    {
        m_adapterPtsExtOutPatientVisitService = adapterPtsExtOutPatientVisitService;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (AuditPrescServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}